package com.bignerdranch.android.criminalintent;

import java.util.ArrayList;
import java.util.UUID;

import android.content.Context;
import android.util.Log;

public class CrimeLab {
	private static final String TAG = "CrimeLab";
    private static final String FILENAME = "crime.json";

    private static CrimeLab sCrimeLab;
	private Context mAppContext;
	
	private ArrayList<Crime> mCrimes;
    private  CriminalIntentJSONSerializer mSerializer;

	private CrimeLab(Context appContext) {
		mAppContext = appContext;
        mSerializer = new CriminalIntentJSONSerializer(mAppContext,"test.json");
        try {
            mCrimes = mSerializer.loadCrimes();
        } catch (Exception e) {
            e.printStackTrace();
            mCrimes = new ArrayList<Crime>();
        }
    }
	
	public static CrimeLab get(Context c) {
		if (sCrimeLab == null) {
			sCrimeLab = new CrimeLab(c.getApplicationContext());
		}
		return sCrimeLab;
	}

	public ArrayList<Crime> getCrimes() {
		return mCrimes;
	}
	
	public Crime getCrime(UUID id) {
		for (Crime c : mCrimes){
			if (c.getId().equals(id)){
				return c;
			}
		}
		return null;
	}

    public void addCrime(Crime c){
        mCrimes.add(c);
    }

    public void deleteCrime(Crime c) {
        mCrimes.remove(c);
    }

    public boolean saveCrimes () {
        try {
            mSerializer.saveCrimes(mCrimes);
            Log.d(TAG, "crimes saved to file");
            return true;
        }catch (Exception e) {
            Log.d(TAG, "Error saving crimes: ", e);
            return false;
        }
    }
}
